﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Xml;
using System.Text;

public partial class DMS_cnjob : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string incidentId = Request.QueryString["id"];  //Sets the Incident ID Number (used to query the Database) to the QueryString that was sent
        incnum_TB.Text = incidentId;
        date_TB.Text = System.DateTime.Now.ToShortDateString();
        

    }

    protected void cncl_btn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Incident.aspx?ID=" + Request.QueryString["id"]);
    }

    protected void submit_btn_Click(object sender, EventArgs e)
    {
        string cnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        string value = dd_Teams.Text;  
        string selectState = "SELECT Team_ID FROM Teams WHERE (TeamName = '"+ value + "')";
        DataSet userDS = new DataSet();
        OleDbConnection db_conn1 = new OleDbConnection(cnStr);
        db_conn1.Open();
        OleDbDataAdapter db_dataAdapter = new OleDbDataAdapter(selectState, db_conn1);
        db_dataAdapter.Fill(userDS);
        db_conn1.Close();

        int team_numb = Int32.Parse(userDS.Tables[0].Rows[0].ItemArray[0].ToString());
        DateTime dt = DateTime.Parse("#" + DateTime.Now.ToShortDateString() + "#");

        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        OleDbConnection db_conn = new OleDbConnection(connString);
        OleDbCommand insertStatement = new OleDbCommand("INSERT INTO [Jobs] ([Incident_ID], [Description], [Date Created], [Assigned Team]) VALUES (@Incident_ID, @Description, @Date_Created, @Assigned_Team)", db_conn);
        insertStatement.Parameters.Add("Incident_ID", Int32.Parse(incnum_TB.Text));
        insertStatement.Parameters.Add("Description", desc_TB.Text);
        insertStatement.Parameters.Add("Date_Created", dt);
        insertStatement.Parameters.Add("Assigned_Team", team_numb);

        db_conn.Open();
        insertStatement.ExecuteNonQuery();
        db_conn.Close();


        ///
        /// The code for updating the teams "working" field  goes here
        ///

        OleDbConnection db_conn2 = new OleDbConnection(connString);
        OleDbCommand updateCom = new OleDbCommand("UPDATE TEAMS SET [CURRENTLY WORKING] = TRUE WHERE ([TEAM_ID] = ?)", db_conn2);
        updateCom.Parameters.Add("TEAM_ID", team_numb);
        db_conn2.Open();
        updateCom.ExecuteNonQuery();
        db_conn2.Close();

        Response.Redirect("Incident.aspx?ID=" + Request.QueryString["id"]);


    }
}